var windowName = "userSign";
var bMap,Page,logs = new List();
apiready = function(){
    onWindowShow(function () {
        log("show")

        Page.testMessage += "【 onWindowShow 代码块已执行】"
        getLocations(Page)
    })
    onWindowHide(function () {
    })
    console.log("userSign page is OK");
    Page = createVM("#Page",{
        //页面数据
        curSite:{},
        type:"in",
        locations:[],
        distance:[],
        minIndex:0,
        minDistance:999999,
        minName:"",
        list:[],
        testMessage:""
    },{
        //页面方法
        "init":function () {
            Page.testMessage += "【init代码块已执行】"
            var _this = this
            logs.setUrl("/app/sign/data").setCbFunc(function (data) {
                stopPullDownRefresh();
                stopPullUpLoadMore();
                _this.list = data
            }).getList()

            var pageData = getData4Page(windowName);
            this.type = pageData.type;
            bMap = api.require('bMap');
            bMap.getLocationServices(function(ret, err) {
                if (ret.enable) {
                    Page.testMessage += "是否开启定位服务:" + JSON.stringify(ret)
                    log(JSON.stringify(ret),"是否开启定位服务");
                } else {
                    showToast("未开启定位功能，可能会影响签到签退！");
                }
            });

        }
        ,signIn:function () {

                //提交信息
                var data = {
                    site:0,
                    type:"in"
                }
                apiPost("/app/user/sign",data,function (res) {
                    showToast(res.message)
                    logs.reload()
                })
        }
        ,signOut:function () {
                var data = {
                    site:0,
                    type:"out"
                }
                apiPost("/app/user/sign",data,function (res) {
                    showToast(res.message)
                    logs.reload()
                })

        }

        ,getLoc:function () {

            Page.testMessage += "【 getLoc 代码块已执行】"
            var _this = this
            var list = this.locations
            bMap.getLocation({
                accuracy: '100m',
                autoStop: false,
                filter: 1
            }, function(ret, err) {
                // Page.testMessage += "<br>定位结果:" + JSON.stringify(ret)
                // Page.testMessage += "<br>定位lon:" + ret.lon
                // Page.testMessage += "<br>定位lat:" + ret.lat
                // Page.testMessage += "<br>定位错误信息:" + JSON.stringify(err)
                //
                // logJson(ret.status)
                if (ret.status) {
                    for (var i in list) {
                        // _this.[i]distance
                        var distance = GetDistance(list[i].Location_lat,list[i].Location_lon,ret.lat,ret.lon)
                        // Page.testMessage += "【距离计算结果:" + distance + "】"
                        _this.distance[i] = distance
                        _this.minName = _this.locations[_this.minIndex].Location_name
                        if (distance <= _this.distance[_this.minIndex]) {
                            _this.minIndex = i;
                            _this.minDistance = (distance).toFixed(1);
                            _this.minName = _this.locations[_this.minIndex].Location_name
                        }
                        if (_this.distance.length === _this.locations.length ) {
                            log(distance,_this.minName)
                        }
                        // bMap.getDistance({
                        //     start: {
                        //         lon: list[i].Location_lon,
                        //         lat: list[i].Location_lat
                        //     },
                        //     end: {
                        //         lon: ret.lon,
                        //         lat: ret.lat
                        //     }
                        // }, function(ret) {
                        //     if (ret.status)
                        //     {
                        //         _this.distance[i] = ret.distance
                        //         _this.minName = _this.locations[_this.minIndex].Location_name
                        //         if (ret.distance < _this.distance[_this.minIndex]) {
                        //             _this.minIndex = i;
                        //             _this.minDistance = (ret.distance).toFixed(1);
                        //             _this.minName = _this.locations[_this.minIndex].Location_name
                        //         }
                        //         if (_this.distance.length === _this.locations.length ) {
                        //             log(ret.distance,_this.minName)
                        //         }
                        //
                        //     }
                        //
                        //     Page.testMessage += "【距离计算结果:" + _this.minDistance + "】"
                        // });
                    }
                } else {
                    _this.minDistance = 9999999999
                    showToast("定位错误请联系管理员")
                }
            });
        }

        ,OnPullDownRefresh:function () {
            startPullDownRefresh()
            logs.reload()
        }
        ,OnPullUpLoadMore:function () {
            startPullUpLoadMore()
            logs.getList()
        }
    })
    Page.init()

}

function getLocations(_this) {

    Page.testMessage += "【 getLocations 代码块已执行】"
    apiGet("/app/location",{},function (res) {
        logJson(res,"所有列表：")
        Page.testMessage += "位置列表:" + JSON.stringify(res)
        _this.locations = res.data
        _this.getLoc();
    })
}

function GetDistance(lat1,lng1,lat2,lng2){

    var radLat1 = Rad(lat1);
    var radLat2 = Rad(lat2);
    var a = radLat1 - radLat2;
    var  b = Rad(lng1) - Rad(lng2);
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
        Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    s = s *6378.137 ;// EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000 *1000; //输出为公里
    //s=s.toFixed(4);
    return s;
}
function Rad(d){
    return d * Math.PI / 180.0;//经纬度转换成三角函数中度分表形式。
}